using DiLu.Expansion.File.Operates.Minio.Configs;
using DiLu.Expansion.File.Operates.Minio.Tools;
using Minio.DataModel.Args;

namespace DiLu.Expansion.File.Operates.Minio;

/// <summary>
///     获取Minio文件流
/// </summary>
public static class GetMinioFile
{
    /// <summary>
    ///     获取Minio文件流
    /// </summary>
    /// <param name="config">获取Minio文件流配置</param>
    /// <returns></returns>
    public static async Task<Stream> GetAsync(this GetMinioFileStreamConfig config)
    {
        if (!await config.MinioClient.CheckBucketAsync(config.BucketName)) return null;
        try
        {
            var fileStream = new MemoryStream();
            await config.MinioClient.GetObjectAsync(new GetObjectArgs()
                .WithBucket(config.BucketName)
                .WithObject(config.FileFullPath)
                .WithCallbackStream(stream => { stream.CopyTo(fileStream); }));
            fileStream.Position = 0;
            return fileStream;
        }
        catch
        {
            return null;
        }
    }
}